$(function(){
    var pagenum=10;
	var fi =1;
	var page=1;
	var main=[[ //标题栏
     {field: 'number', title: '车牌号', width: 150,fixed: 'left'}
      ,{field: 'car_type_name', title: '车型', minWidth: 150}
      ,{field: 'brand_name', title: '品牌', minWidth: 150}
      ,{field: 'company_name', title: '所属公司', minWidth: 200}                          
      ,{field: 'engine_no', title: '发动机号', width: 180}
      ,{field: 'displacement', title: '排量', minWidth: 150}
      ,{field: 'source2', title: '来源', minWidth: 150}
      ,{field: 'energy_type2', title: '能源类型', width: 150}
      ,{field: 'buy_money', title: '保险费用', minWidth: 150}
      ,{field: 'buy_date', title: '购买日期', minWidth: 150}
      ,{field: 'colour', title: '车辆颜色', width: 150}
      ,{field: 'car_age', title: '车龄', minWidth: 150}
      ,{field: 'mileage', title: '行驶里程', minWidth: 150}
      ,{field: 'gearbox_type2', title: '变速箱类型', width: 150}
      ,{field: 'carry', title: '承载人数', minWidth: 150}
      ,{field: 'structure2', title: '车身结构', minWidth: 150}
      ,{field: 'prop', title: '车辆性质', minWidth: 150}
      ,{field: 'driving_license_issued', title: '行驶证发放日期', width: 150}
      ,{field: 'buy_money', title: '购买金额', minWidth: 150}
      ,{field: 'entrust_company_name', title: '委托公司（租车）', width: 150}
      ,{field: 'rent_company', title: '租赁公司', minWidth: 150}
      
    ]]
	
	// var addoperation=``;
	// var fields='';
	
	var data={
		page:1,
		pagenum:10
	}
	
	var where_json={}
	{if $Think.get.number}
		where_json.number=['like','%{$Think.get.number}%']
	{/if}
	{if $Think.get.company_name}
		where_json.company_name=['like','%{$Think.get.company_name}%']
	{/if}
	{if $Think.get.car_type_name}
		where_json.car_type_name=['like','%{$Think.get.car_type_name}%']
	{/if}
	{if $Think.get.is_check}
		where_json.is_check=['in','{$Think.get.is_check}']
	{/if}
	
	data.where_json=JSON.stringify(where_json)
	ajaxcar(data)
	function ajaxcar(data){
		Load()
		$.ajax({
			type:'post',
			data:data,
			url:api.car.list,
			success:function(res){
				Load(1)
				console.log(res)
				if(res.code==1){
				for(var i=0;i<res.list.length;i++){
					if(res.list[i].source==1){
						res.list[i].source2='单位直属公务车'
					}else if(res.list[i].source==2){
						res.list[i].source2='管理公司直属公务车'
					}else if(res.list[i].source==3){
						res.list[i].source2='单位自有车辆'
					}else{
						res.list[i].source2='来源未知'
					}
					if(res.list[i].energy_type==1){
						res.list[i].energy_type2='电动'
					}else if(res.list[i].energy_type==2){
						res.list[i].energy_type2='汽油'
					}else if(res.list[i].energy_type==3){
						res.list[i].energy_type2='柴油'
					}else{
						res.list[i].energy_type2='类型未知'
					}
					if(res.list[i].gearbox_type==1){
						res.list[i].gearbox_type2='手动'
					}else if(res.list[i].gearbox_type==2){
						res.list[i].gearbox_type2='自动'
					}else if(res.list[i].gearbox_type==3){
						res.list[i].gearbox_type2='手自一体'
					}else{
						res.list[i].gearbox_type2='类型未知'
					}
					if(res.list[i].structure==1){
						res.list[i].structure2='两厢车'
					}else if(res.list[i].structure==2){
						res.list[i].structure2='三厢车'
					}else if(res.list[i].structure==3){
						res.list[i].structure2='SUV'
					}else{
						res.list[i].structure2='车身结构未知'
					}
					res.list[i].car_type_name=res.list[i].cartype.car_type_name
					res.list[i].company_name=res.list[i].company.company_name
					res.list[i].entrust_company_name=res.list[i].entrust.company_name
				}	
				 //展示已知数据
				 	 table.render({
				    	elem: '#car'
					    ,cols: main
					    ,data:res.list
					    ,even: true
					    ,limit: data.pagenum
				  	});
					  //   分页
					  if (fi == 1) {
						laypage.render({
							elem: 'page',
							count: res.count,
							layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip'],
							theme: '#1E9FFF',
							jump: function (obj, first) {
								console.log(obj)
								if (!first) {
									page = obj.curr;
									data.page = page;
									pagenum = obj.limit;
									data.pagenum = pagenum;
									ajaxcar(data);
								} else {
			
								}
							}
						})
						$('.daochu').on('click',function(){
							var getkey="";
							for(var iii=0;iii<main[0].length;iii++){
								if(main[0][iii].field=='source2'){
									getkey+=`<li>
											<label class="am-checkbox">
											  <input type="checkbox" value="" data-key='source' checked> ${main[0][iii].title}
											</label>
										</li>`
								}else if(main[0][iii].field=='energy_type2'){
									getkey+=`<li>
											<label class="am-checkbox">
											  <input type="checkbox" value="" data-key='energy_type' checked> ${main[0][iii].title}
											</label>
										</li>`
								}else if(main[0][iii].field=='gearbox_type2'){
									getkey+=`<li>
											<label class="am-checkbox">
											  <input type="checkbox" value="" data-key='energy_type' checked> ${main[0][iii].title}
											</label>
										</li>`
								}else if(main[0][iii].field=='structure2'){
									getkey+=`<li>
											<label class="am-checkbox">
											  <input type="checkbox" value="" data-key='structure' checked> ${main[0][iii].title}
											</label>
										</li>`
								}else{
								getkey+=`<li>
											<label class="am-checkbox">
											  <input type="checkbox" value="" data-key='${main[0][iii].field}' checked> ${main[0][iii].title}
											</label>
										</li>`
								}
//								
								
							}
							console.log(getkey)
							layer.open({
								  type: 1,
								  title:false,
								  skin: 'layui-layer-rim', //加上边框
								  area: ['300px', '400px'], //宽高
								  content: `<div>
											 <div style='height:300px;overflow:auto;margin-bottom:30px'>
											  <ul class='daochuul'>${getkey}</ul>
											  </div>
											  <div style='text-align:center'>
											  <button class="layui-btn layui-btn-normal daochu_ok" type='button'>确定导出</button>
											  </div>
										</div>`,
								success:function(){
									var ddata={}
									$('.daochu_ok').on('click',function(){
										ddata.fields="";
										var where_jsondao={};
										{if $Think.get.name}
											where_jsondao.name=['like','%{$Think.get.name}%']
										{/if}
										{if $Think.get.phone}
											where_jsondao.phone=['like','%{$Think.get.phone}%']
										{/if}
										{if $Think.get.company_name}
											where_jsondao.company_name=['like','%{$Think.get.company_name}%']
										{/if}
										{if $Think.get.number}
											where_jsondao.number=['like','%{$Think.get.number}%']
										{/if}
										
										ddata.where_json=JSON.stringify(where_jsondao)
										
										$('.daochuul li input[type="checkbox"]').each(function(){
											if(this.checked){
												ddata.fields+=($(this).data('key')+",")
											}
										})
										ddata.fields=ddata.fields.slice(0,ddata.fields.length-1)
										console.log(ddata.fields)
										window.open(`${api.car.down}?where_json=${ddata.where_json}&fields=${ddata.fields}`)								
									})
									
								}
							});
						})
						fi = 2;
					}


				}else{
					Err(res.msg)
				}
			},
			error:function(){
				Err('请求失败')
			}
		})
	}
})
